######################################################################
# Makefile - build the project
######################################################################
TMP=tmp
SRC=src
OUT=out
EXT=external

COMPILER_URL=http://closure-compiler.googlecode.com/files/compiler-latest.zip
COMPILER_DIR=compiler
COMPILER_ZIP=compiler-latest.zip
COMPILER_BIN=$(EXT)/$(COMPILER_DIR)/compiler.jar
#COMPILER_LEVEL="WHITESPACE_ONLY"
COMPILER_LEVEL="SIMPLE_OPTIMIZATIONS"
#COMPILER_LEVEL="ADVANCED_OPTIMIZATIONS"

SH_TAR_URL=https://github.com/alexgorbatchev/SyntaxHighlighter/tarball/3.0.83
SH_DIR=SyntaxHighlighter-3.0.83
SH_OUTPUT=$(EXT)/$(SH_DIR)/build/output
SH_CORE=--js $(SH_OUTPUT)/scripts/shCore.js

YUI_VER=2.4.7
YUI_DIR=yuicompressor-$(YUI_VER)
YUI_URL=http://yui.zenfs.com/releases/yuicompressor/$(YUI_DIR).zip
YUI_BIN=$(EXT)/$(YUI_DIR)/$(YUI_DIR)/build/$(YUI_DIR).jar

UPLOAD_URL=http://support.googlecode.com/svn/trunk/scripts/googlecode_upload.py
UPLOAD_DIR=upload
UPLOAD_BIN=$(EXT)/$(UPLOAD_DIR)/googlecode_upload.py

USER=$(shell cut -d" " -f 4 ~/.netrc)
PASS=$(shell cut -d" " -f 6 ~/.netrc)

ifeq ($(strip $(PROJECT)),)
	PROJECT=$(shell git remote show -n origin | sed -n '/^  Fetch/s,.*/,,p')
	ifeq ($(strip $(PROJECT)),)
		PROJECT = $(error cant calculate PROJECT)
	 endif
endif
ifeq ($(strip $(BRUSHES)),)
	BRUSHES = $(shell [ -d $(SH_OUTPUT)/scripts ] && find $(SH_OUTPUT)/scripts -name "shBrush*js" -printf "%f\n" | sort | sed -e 's/shBrush//' -e 's/\.js//')
	ifeq ($(strip $(BRUSHES)),)
		PROJECT = $(error Do a make get-external)
	 endif
endif
OUT_URL=http://$(PROJECT).googlecode.com/files
SH_BRUSHES=$(foreach x,$(BRUSHES),--js $(SH_OUTPUT)/scripts/shBrush$(x).js)

help::
	@echo "=============================================================="
	@echo "$(PROJECT) help"
	@echo "=============================================================="
	@echo "vars                   Display variable values"
	@echo "squeeky                Start fresh"
	@echo "clean                  Clean output and tmp"
	@echo "get-external           Download external code"
	@echo "output                 Build the output files (BRUSHES)"
	@echo "upload                 Upload the output files"
	@echo "git-status             Status of the repo."
	@echo "git-commit             Commit changes."
	@echo "git-push               Commit push changes back to google code."

vars::
	$(info PROJECT=$(PROJECT))
	$(info BRUSHES=$(BRUSHES))
	$(info USER=$(USER))

squeeky::
	@$(MAKE) --no-print-directory clean
	@rm -rf $(EXT)

clean::
	@rm -rf $(OUT)

get-external::
	@rm -rf $(EXT)
	@$(MAKE) --no-print-directory get-sh
	@$(MAKE) --no-print-directory get-compiler
	@$(MAKE) --no-print-directory get-yui
	@$(MAKE) --no-print-directory get-upload

get-sh::
	@echo $(SH_DIR)
	@rm -rf $(EXT)/$(SH_DIR)
	@mkdir -p $(EXT)/$(SH_DIR)
	@wget -q $(SH_TAR_URL) -O - | tar -C $(EXT)/$(SH_DIR) -xzf - --xform 's,^[^/]*/,./,'
	@sed -i '/PHP_COMMAND/s,.*,export PHP_COMMAND='/opt/lampp/bin/php',' $(EXT)/$(SH_DIR)/build/build
	@(cd $(EXT)/$(SH_DIR)/build && ./build > build.log 2>&1 )

get-compiler::
	@echo $(COMPILER_DIR)
	@mkdir -p $(EXT)/$(COMPILER_DIR)
	@wget -q $(COMPILER_URL) -O $(EXT)/$(COMPILER_DIR)/$(COMPILER_ZIP)
	@(cd $(EXT)/$(COMPILER_DIR)/ && unzip $(COMPILER_ZIP) >/dev/null )

get-yui::
	@echo $(YUI_DIR)
	@mkdir -p $(EXT)/$(YUI_DIR)
	@wget -q $(YUI_URL) -O $(EXT)/$(YUI_DIR)/$(YUI_VER).zip
	@(cd $(EXT)/$(YUI_DIR)/ && unzip $(YUI_VER).zip >/dev/null )

get-upload::
	@echo $(UPLOAD_DIR)
	@mkdir -p $(EXT)/$(UPLOAD_DIR)
	@wget -q $(UPLOAD_URL) -O $(UPLOAD_BIN)
	@chmod a+x $(UPLOAD_BIN)

output::
	@[ -n "$(BASEURL)" ] || ( echo >&2 you must set BASEURL ; false )
	@rm -rf $(TMP) $(OUT)
	@mkdir -p $(TMP) $(OUT)
	@sed 's,BASEURL,$(BASEURL),' < $(SRC)/highlightloader.js > $(TMP)/highlightloader.js
	@cat $(EXT)/$(SH_DIR)/styles/shCoreEclipse.css | java -jar $(YUI_BIN) --type css -o $(OUT)/syntaxhighlighter.css
	@java -jar $(COMPILER_BIN) --compilation_level $(COMPILER_LEVEL) $(SH_CORE) $(SH_BRUSHES) --js $(SRC)/dohighlight.js --js_output_file $(OUT)/syntaxhighlighter.js
	@java -jar $(COMPILER_BIN) --compilation_level $(COMPILER_LEVEL) --js $(TMP)/highlightloader.js --js_output_file $(OUT)/highlightloader.js

upload::
	@for x in $(OUT)/*; do $(UPLOAD_BIN) -p $(PROJECT) -s `basename $$x` -u $(USER) -w $(PASS) $$x; done

git-status::
	@git status

git-commit::
	@git commit

git-push::
	@git push origin master

# Snippets:
#YUI_JS=$(foreach x,$(BRUSHES),$(EXT)/$(SH_DIR)/scripts/shBrush$(x).js )
#@cat $(EXT)/$(SH_DIR)/scripts/shCore.js $(YUI_JS) $(SRC)/dohighlight.js | java -jar $(YUI_BIN) --type js -o $(OUT)/syntaxhighlighter.js
#@java -jar $(COMPILER_BIN) --compilation_level $(COMPILER_LEVEL) --js $(SRC)/top.js $(SH_BRUSHES) --js $(SRC)/dohighlight.js --js $(SRC)/bot.js --js_output_file $(OUT)/syntaxhighlighter.js
# vim:ts=4 
